from pydantic import BaseModel, Field, model_validator
from application import settings
from application.utils import tools

class UserRegisterRequest(BaseModel):
    """小程序用户注册请求"""
    mobile: str = Field(pattern=r'^1[3-9]\d{9}$', description="手机号")
    password: str = Field(min_length=6, max_length=20, description="密码")
    sms_code: str = Field(max_length=settings.SMS['CODE_LENGTH'], description="短信验证码")
    nickName: str = Field(description="微信昵称")
    avatarUrl: str = Field(description="微信头像")
    gender: int = Field(description="性别")
    country: str = Field(description="国家")
    province: str = Field(description="省份")
    city: str = Field(description="城市")
    language: str = Field(description="语言")
    code: str = Field(description="小程序码")

    @model_validator(mode="after")
    def model_validate(self):
        # 对密码进行哈希加密
        hashing = tools.Hashing()
        self.password = hashing.hash(self.password)
        return self

class BaseResponse(BaseModel):
    """响应基类"""
    code: int = Field(description="响应码")
    message: str = Field(description="响应信息")
    status: str = Field(description="响应状态")

class UserRegisterResponse(BaseResponse):
    """小程序用户注册响应"""
    id: int = Field(description="用户id")
    avatar: str = Field(description="用户头像")
    nickname: str = Field(description="用户昵称")
    token: str = Field(description="用户token")


class UserLoginRequest(BaseModel):
    """登陆接口的请求数据结构"""
    mobile: str = Field(pattern=r'^1[3-9]\d{9}$', description="手机号")
    password: str = Field(min_length=6, max_length=16, description='密码')
    sms_code: str = Field(max_length=settings.SMS['CODE_LENGTH'], description='短信验证码')
    code: str = Field(description='小程序码')
